package com.soft.controller;

import com.soft.pojo.Position;
import com.soft.service.PositionService;
import com.soft.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-28 14:33
 */
@WebServlet("/addJob")
public class AddJobServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String gname = req.getParameter("gname");
        String gwzz = req.getParameter("gwzz");
        int num = Integer.parseInt(req.getParameter("num"));
        String superior = req.getParameter("superior");
        String subordinates = req.getParameter("subordinates");

        String[] ask = req.getParameterValues("ask");
        StringBuilder as = new StringBuilder();
        for (String s : ask) {
            as.append(s);
            as.append(",");
        }
        String asks = as.substring(0, as.length()-1);

        String wages = req.getParameter("wages");

        Position position = new Position();
        position.setGname(gname);
        position.setGwzz(gwzz);
        position.setNum(num);
        position.setSuperior(superior);
        position.setSubordinates(subordinates);
        position.setAsk(asks);
        position.setWages(wages);

        PositionService positionService = new PositionService();
        Result result = positionService.addPosition(position);

        req.setAttribute("addJobMsg", result.getMsg());
        req.getRequestDispatcher("showAllPositionByPage").forward(req,resp);
    }
}
